package com.example.lin.canvas_paint;

import android.Manifest;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.DiscretePathEffect;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.Xfermode;
import android.os.Handler;
import android.service.quicksettings.Tile;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import static android.view.View.LAYER_TYPE_SOFTWARE;

public class MainActivity extends AppCompatActivity {

    private int widthPixels;
    private ImageView iv_show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_show = findViewById(R.id.iv_show);
        widthPixels = getResources().getDisplayMetrics().widthPixels;
    }

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        studyDrawing();

    }

    private void studyDrawing() {


        Bitmap bitmap = Bitmap.createBitmap(widthPixels, widthPixels, Bitmap.Config.ARGB_8888);
        //创建画布
        Canvas canvas = new Canvas(bitmap);

        //支持画,点,线,矩形,圆,圆角矩形,椭圆,弧度,路径,文字,画Bitmap

        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.FILL);
//        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);//抗锯齿(特别体现在圆弧的时候)
        paint.setStrokeWidth(20);
        paint.setStrokeCap(Paint.Cap.ROUND);//使线条端点圆润
//        paint.setStrokeJoin(Paint.Join.ROUND);//使线条端点圆润


        /**画点**/
        //画一个点
//        canvas.drawPoint(200, 200, paint);
        //画多个点
//        float[] fs = {100, 100, 200, 200, 100, 200, 200, 100};//每两个值确定1个点.必须是偶数个
//        canvas.drawPoints(fs, paint);

        /**画线**/
        //画1条线
//        canvas.drawLine(100, 100, 600, 600, paint);
        //画多条线
//        float[] pts = {500, 0, 200, 600, 200, 600, 800, 600, 800, 600, 500, 0};//没4个值确定1条线,必须是4的整数倍
//        canvas.drawLines(pts, paint);

        /**画矩形**/

//        LinearGradient linearGradient = new LinearGradient(100, 100, 200, 200, Color.RED, Color.BLUE, Shader.TileMode.MIRROR);
//
//        SweepGradient sweepGradient = new SweepGradient(350, 350, Color.RED, Color.GREEN);

        //设置阴影
//        paint.setShadowLayer(20, -20,-20,Color.GRAY);
//        iv_show.setLayerType(LAYER_TYPE_SOFTWARE, paint);//开启硬件加速


//        RadialGradient radialGradient = new RadialGradient(350, 350, 100, Color.RED, Color.GREEN, Shader.TileMode.MIRROR);
//        paint.setShader(radialGradient);
//        canvas.drawRect(100,100,600,600,paint);

        //对矩形left,top,right,bottom的封装,只能是int值
//        Rect rect = new Rect(100, 100, 600, 400);
        //对矩形left,top,right,bottom的封装,可以是float类型
//        RectF rect = new RectF(100.5f, 100, 600.5f, 400);
//        canvas.drawRect(rect,paint);

        /**画圆**/
//        canvas.drawCircle(500, 500, 400, paint);

        /**画圆角矩形*/
//        RectF rect = new RectF(100, 100, 600, 400);
//        canvas.drawRoundRect(rect,250,150,paint);

        /**画椭圆*/
//        RectF rect = new RectF(100, 100, 600, 400);
//        canvas.drawOval(rect, paint);

        /**画弧线**/
//        RectF rect = new RectF(100, 100, 600, 600);
//        canvas.drawArc(rect, -10, -160, false, paint);
//


        /***画路径*/


//        Path path = new Path();
//
//        path.moveTo(100,100);//设置起点
//
//        path.lineTo(300,100);
//        path.lineTo(300,300);
//        path.lineTo(500,300);
//        path.lineTo(500,500);
//        path.lineTo(700,500);
//        path.lineTo(700,100);

//        path.moveTo(100,100);//设置起点
//        path.rMoveTo(100,100);//设置起点
//        path.rLineTo(200,0);//

//        path.quadTo(100,500,300,500);//

//        path.moveTo(100,300);
//        RectF rectF = new RectF(100, 500, 180, 580);
//        path.arcTo(rectF,180,-180);
//        path.lineTo(180,300);
//        path.arcTo(new RectF(100, 200, 180, 280),180,180);

//        path.close();//闭合路径,让终点与起点连接
//        path.close();

//        path.addCircle(300,300,200, Path.Direction.CW);
//        path.addCircle(300,300,150, Path.Direction.CCW);
//        Path p = new Path();
//        p.moveTo(100,100);
//        p.lineTo(600,600);
//        path.addPath(p);

//        paint.setPathEffect(new CornerPathEffect(50));
//        paint.setPathEffect(new DiscretePathEffect(3f, 20));
//        paint.setPathEffect(new DashPathEffect(new float[]{30,20},0));
//        Path c = new Path();
//        c.addCircle(0,0,10, Path.Direction.CW);
//        paint.setPathEffect(new PathDashPathEffect(c, 40,0, PathDashPathEffect.Style.ROTATE));

//        canvas.drawPath(path,paint);//绘制路径


        /**画文本,掌握 字体的 BaseLine 基线**/
//        canvas.drawRect(new RectF(100, 100, 800, 200),paint);
//        Paint textPain = new Paint();
//        textPain.setColor(Color.RED);
//        textPain.setTextSize(DensityUtils.sp2px(this, 24));
//
//        Paint.FontMetrics fontMetrics = textPain.getFontMetrics();
//
//        Log.d("TAG", "studyDrawing: " + fontMetrics.top);
//        Log.d("TAG", "studyDrawing: " + fontMetrics.bottom);
//        Log.d("TAG", "studyDrawing: " + fontMetrics.leading);
//        Log.d("TAG", "studyDrawing: " + fontMetrics.ascent);
//        Log.d("TAG", "studyDrawing: " + fontMetrics.descent);
//
////        canvas.drawText("good 爱 Bye!", 100, 300, textPain);
////        canvas.drawLine(0,300,1000,300,paint);
////        canvas.drawLine(100,150,100,500,paint);
//
//        float baselineY = 150 + (fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent;
//
//        canvas.drawText("good 爱 Bye!", 100, baselineY, textPain);
        /**画文本到路径上*/

//        Path pp = new Path();

//        pp.moveTo(100,100);
//        pp.rLineTo(100,100);
//        pp.rLineTo(100,-100);
//        pp.rLineTo(100,100);
//        pp.addCircle(400,400,300, Path.Direction.CCW);
//        pp.arcTo(new RectF(100, 100, 700, 700), 200, 140);
//
//        canvas.drawPath(pp, paint);
//
//        //获取 文本的总长度(px),textPain必须先设置 textSize
//        float v = textPain.measureText("good Bye!Very Good!");
//
//        Log.d("TAG", "studyDrawing: " + v);
//        //计算字体的高度px
//        Paint.FontMetrics fontMetrics = textPain.getFontMetrics();
//        float h = fontMetrics.bottom - fontMetrics.top;
//        Log.d("TAG", "studyDrawing: " + h);

//        textPain.setUnderlineText(true);
//        textPain.setStrikeThruText(true);
//        textPain.setFakeBoldText(true);
//        textPain.setTextSkewX(-0.5f);
//        textPain.setTextScaleX(0.5f);
//        textPain.setTextAlign(Paint.Align.LEFT);
//        canvas.drawTextOnPath("good Bye!Very Good!", pp, 80, 10, textPain);
//        canvas.drawText("good Bye!Very Good!", 100,200, textPain);


//        canvas.drawCircle(300, 300, 200, paint);


        //橡皮擦效果
//        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));


//        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//        paint.setColor(Color.parseColor("#AA00ff00"));
//        canvas.drawRect(100,100,500,500,paint);

        /**画位图 Bitmap*/
//        Bitmap x = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
//
//        Canvas xc = new Canvas(x);
//        xc.drawCircle();

//        Bitmap ss = BitmapFactory.decodeResource(getResources(), R.mipmap.ss);
//
////        Bitmap scaledBitmap = Bitmap.createScaledBitmap(ss, widthPixels, widthPixels, true);
////        canvas.drawBitmap(ss, 0, 0, null);
//
//        Matrix matrix = new Matrix();
////        matrix.postRotate(90,ss.getWidth()/2,ss.getHeight()/2);
////        matrix.postScale(2,2,ss.getWidth()/2,ss.getHeight()/2);
////        matrix.postTranslate(100, 0);
//        matrix.setSkew(0.2f, 0.2f);
////        matrix.setSkew(0.2f,0.2f,ss.getWidth()/2,ss.getHeight()/2);
////        canvas.drawBitmap(ss, matrix, null);
//
//        //挖取 Bitmap的那一块内容
//        Rect rect = new Rect(0, 0, ss.getWidth(), ss.getHeight());
//        //需要将 Bitmap 绘制到画布的那一块区域
//        RectF rectF = new RectF(0, 0, widthPixels, widthPixels);
////
//        canvas.drawBitmap(ss, rect, rectF, null);


        /**画板的 旋转,缩放,平移,倾斜**/
//        int cx = widthPixels / 2;
//        int cy = cx;
//
//        canvas.drawLine(100, 100, 900, 100, paint);
//        canvas.drawCircle(cx, cy, 15, paint);
//
//        paint.setStrokeWidth(20);
//        paint.setStyle(Paint.Style.STROKE);
//        canvas.drawCircle(cx, cy, 400, paint);

//        canvas.rotate(d,cx,cy);
//        canvas.scale(0.5f,0.5f,cx,cy);
//        canvas.translate(0,500);
//        canvas.skew(0.2f,0.2f);


//        canvas.save();//保存画布的状态
//        canvas.rotate(d, cx, cy);
//        paint.setStrokeWidth(5);
//        canvas.drawLine(cx, cy, cx, cy - 300, paint);
//        canvas.drawLine(cx, cy, cx, cy - 300, paint);
//        canvas.drawLine(cx, cy, cx, cy - 300, paint);
//        canvas.drawLine(cx, cy, cx, cy - 300, paint);
//        canvas.drawLine(cx, cy, cx, cy - 300, paint);
//        canvas.restore();//将画布恢复到最近的一次save()状态
//
//
//        canvas.save();//保存画布的状态
//        canvas.rotate(f, cx, cy);
//        paint.setStrokeWidth(10);
//        paint.setColor(Color.RED);
//        canvas.drawLine(cx, cy, cx, cy - 250, paint);
//        canvas.restore();//将画布恢复到最近的一次save()状态
//
//        canvas.save();//保存画布的状态
//        canvas.rotate(s, cx, cy);
//        paint.setStrokeWidth(20);
//        paint.setColor(Color.YELLOW);
//        canvas.drawLine(cx, cy, cx, cy - 200, paint);
//        canvas.restore();//将画布恢复到最近的一次save()状态
//
//
//        iv_show.setImageBitmap(bitmap);
//
//        d += 3600*190;
//        f = d / 60;
//        s = f / 12;
//
//        h.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                studyDrawing();
//            }
//        },10);
//    }
//
//    private float d = 0;
//    private float f = 0;
//    private float s = 0;
//
//    private Handler h = new Handler();
    }


}








